package com.hp.bon.sgw.wh.mml.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Properties;

import com.hp.bon.sgw.wh.mml.repositories.PropertyRepository;

public class PropertiesHelper {

	private Properties proterties = null;
	private String propertiesName = null;
	private long lastModified = Long.MIN_VALUE;
	
	public static PropertiesHelper propertiesLoader = null;
	
	public static void loadZBVCProperties(){
		if(propertiesLoader==null){
			propertiesLoader = new PropertiesHelper();			
		}
		
		String propertiesName ="./etc/zbvc.properties";
		if(propertiesLoader.loadProperties(propertiesName)){
			propertiesLoader.buildPropertiesRepository();			
		}
	}
	
	/**
	 * 
	 */
	public static void loadBlackCardProperties(){
		if(propertiesLoader==null){
			propertiesLoader = new PropertiesHelper();			
		}
		String propertiesName ="./etc/blackcard.properties";
		if(propertiesLoader.loadProperties(propertiesName)){
			propertiesLoader.buildPropertiesRepository();			
		}
	}
	
	private PropertiesHelper(){
	}
	
	/*
	 *  if return true: need rebuild PropertyRepository
	 */
	public boolean loadProperties(String propertiesName){
		this.propertiesName = propertiesName;
		/*if(null==propertiesName){
			//String classPath = this.getClass().getClassLoader().getResource("/").getPath();
			String classPath = "./etc/";
			propertiesName = classPath + "zbvc.properties";
		}*/		
		//检查文件是否修改过,若修改过,则需要重新加载Properties文件
		File file = new File(propertiesName);
		long currenLastModified = file.lastModified();		
		if(this.lastModified < currenLastModified){
			if(proterties==null){
				proterties = new Properties();				
			} else if(!checkPropertiesRefresh()){
				return false;
			}
			InputStreamReader reader = null;
			try {
				reader = new InputStreamReader(new FileInputStream(propertiesName),"UTF-8");
				proterties.load(reader);
				this.lastModified = currenLastModified;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if(reader!=null){
						reader.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return true;
		}
		return false;
	}
	
	public void buildPropertiesRepository(){
		
		Enumeration<?> propertyNames = proterties.propertyNames();
		while(propertyNames.hasMoreElements()){
			String name = (String)(propertyNames.nextElement());
			PropertyRepository.addProperty(name, (String)(proterties.getProperty(name)));
		}
		
	}
	
	public boolean checkPropertiesRefresh(){
		boolean ret = false;
		String tmpName = this.propertiesName + ".refresh";
		File file = new File(tmpName);
		try {
			if(file.exists()){
				ret = true;
				file.delete();
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		return ret;
	}

}
